<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>管理平台</title>
    <jsp:include page="../../include/header.jsp"></jsp:include>
</head>
<body>
<div id="wrapper">

		<div class="row">
			<div class="col-lg-12">
				<h1 class="page-header">用户</h1>
			</div>
		</div>
		<!-- 搜索框 -->
		<div class="row">
			<div class="col-lg-offset" style="margin-left: 10%;" >
				<label class="search-label">电话：</label><input type="text" id="sPhone" value="" placeholder="请输入用户手机号" class="form-control search-input width200" maxlength="21">
				<label class="search-label">姓名：</label><input type="text" id="sName" value="" placeholder="请输入用户姓名" class="form-control search-input width200" maxlength="21">
				<c:if test="${RoleId==1}" >
					<label class="search-label">商家：</label>
					<select id="sBusId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="12%">
						<c:forEach items="${busList }" var="bus">
							<option value="${bus.sellerId }">${bus.sellerName }</option>
						</c:forEach>
					</select>
					<label class="search-label">营业部：</label>
					<select id="sStoreId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="12%" ></select>
				</c:if>
				<c:if test="${RoleId==2}">
					<label class="search-label">营业部：</label>
					<select id="sStoreId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="12%" >
						<c:forEach items="${StoreList }" var="store">
							<option value="${store.storeid }">${store.title }</option>
						</c:forEach>
					</select>
				</c:if>
				<c:if test="${RoleId==1 ||RoleId==2 ||RoleId==3}">
					<label class="search-label">职位：</label>
					<select id="sJob" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="12%">
						<%-- <c:forEach items="${jobList }" var="job">
							<option value="${job.id }">${job.alias }</option>
						</c:forEach> --%>
						<option value="">请选择</option>
						<c:if test="${RoleId==1}">
						<option value="${jobList[1].id }">${jobList[1].alias }</option></c:if>
						<c:if test="${RoleId==1||RoleId==2}">
						<option value="${jobList[2].id }">${jobList[2].alias }</option></c:if>
						<option value="${jobList[3].id }">${jobList[3].alias }</option>
					</select>
				</c:if>
			</div>			
			<div class="col-lg-offset">
				<button class="btn btn-success search-btn" onclick="query();">确定</button>
				<button class="btn btn-success search-btn" onclick="reset();">重置</button>
			</div>
		</div>
		<div class="row">
			<div class="col-lg-12">
				<div class="row">
					<div class="col-lg-6">
						<div class="text-muted single-line-text pull-left">共 <font color="#428bca" id="dataCount">0</font> 条记录</div>
					</div>
					<div class="col-lg-6">
						<div class="pull-right">
						<button type="button" class="btn btn-link" onclick="toAdd()"><span class="fa fa-plus"></span> 添加</button>
						</div>
					</div>
				</div>
				<div class="table-responsive">
					<table id="dataTable" class="table table-striped table-hover">
			            <thead>
			                <tr>
			                    <th width="20%">真实姓名</th>
			                    <th width="20%">账号</th>
			                    <th width="20%">用户手机号</th>
			                    <th width="20%">所属角色</th>
			                    <th width="20%">所属分公司</th>
			                    <th width="20%">添加时间</th>
			                    <th width="25%">操作</th>
			                </tr>
			            </thead>
			            <tbody id="dataList"></tbody>
			        </table>
				</div>
		        <div class="row">
		        	<div class="col-lg-12">
		        		<ul id="dataPagination" class="pagination-sm pull-right"></ul>
		        	</div>
		        </div>
			</div>
		</div>
</div>
<jsp:include page="../../include/javascripts.jsp"></jsp:include>
<!-- 异步加载下一页数据后，用模板渲染 --><!-- <td>{{item.enabled | flagTransform:1,'可用',0,'禁用'}}</td> -->
<script type="text/html" id="dataTpl">
	{{each list as item}}
	<tr id="item{{item.id}}" data-id="{{item.id}}">
		<td>{{item.trueName}}</td>
		<td>{{item.username}}</td>
        <td>{{item.phonenumber}}</td>
        <td>{{item.aname}}</td>
        <td>{{item.title}}</td>
		<td>{{item.createDate | dateFormat:'yyyy-MM-dd'}}</td>
		<td>
			{{if ${currUser.id}!=item.id}}
					<button type="button" class="btn btn-sm btn-danger btn-simple" onclick="toEditPwd('{{item.id}}')" title="修改密码"><span class="fa fa-lock fa-lg"></span></button> 
			{{/if}}
				<button type="button" class="btn btn-sm btn-primary btn-simple" onclick="toEdit('{{item.id}}')" title="编辑"><span class="fa fa-edit fa-lg"></span></button>
			
			<button type="button" class="btn btn-sm btn-primary btn-simple" onclick="toAccredit('{{item.id}}')" title="授权"><span class="fa fa-info fa-lg"></span></button></td>
	</tr>
 
	{{/each}}
</script>
<script type="text/javascript">
var dataPaginator;
$(query);
function query(){
	 var tel=$("#sPhone").val(),name=$("#sName").val(),seller=$("#sBusId").val(),store=$("#sStoreId").val(),job=$("#sJob").val();
	 var sellers="";
		if(seller!=null && job!="" && seller!=undefined){
			sellers=seller.join("','");
		} 
	var stores="";
		if(store!=null && store!="" && store!=undefined){
			stores=store.join("','");
		} 
	 var jobs="";
		if(job!=null && job!="" && job!=undefined){
			  jobs=job.join("','");
		} 	 
	 var params = {
		pageSize:10,
		tel:tel,
		sellers:sellers,
		jobs:jobs,   
		stores:stores, 
		name:name  
	};
	if(dataPaginator){
		dataPaginator.destroy();
	}
	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/auth/admin/page",params,function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
	});
}
function reset(){
	$("#sPhone").val("");$("#sName").val("");$("#sBusId").val("");$("#sStoreId").val("");$("#sJob").val("");
	$("#sBusId").trigger("change");$("#sStoreId").trigger("change");$("#sJob").trigger("change");
	$("#sBusId").selectpicker("refresh");$("#sStoreId").selectpicker("refresh");$("#sJob").selectpicker("refresh");
}
$(function(){
	if(dataPaginator){
		dataPaginator.destroy();
	}
	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/auth/admin/page",{"pageSize":10},function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
	});
});
var addDialog;
function toAdd(){
	var state=${state};
	var power=${power};
	var RoleId=${RoleId};
	var qstate=${qstate};
	if(RoleId==1){
		 addDialog = Kit.dialog("添加","${BASE_PATH}/auth/admin/toAdd" ,{closable:false}).open();
	}else if(RoleId==2){
		
	   if(state==2&&power==1){
		   addDialog = Kit.dialog("添加","${BASE_PATH}/auth/admin/toAdd" ,{closable:false}).open();
	   }
	   if(state!=2){
			Kit.confirm("提示","请您先提交商家入驻信息")
	   }
	   if(power!=1){
		   Kit.confirm("提示","您的商家权限被关闭，请联系管理员")
	    }
		
	}else{
		if(state==2&&qstate==1) {
			addDialog = Kit.dialog("添加","${BASE_PATH}/auth/admin/toAdd" ,{closable:false}).open();
		}else{
			Kit.confirm("提示","您的营业部权限被关闭，请联系管理员")
		}
	}
	
}
var editDialog;
function toEdit(id){
	editDialog = Kit.dialog("修改","${BASE_PATH}/auth/admin/toEdit/"+id).open();
}
function onEditSuccess(id,newObject){
	$("#item"+id).replaceWith(template("dataTpl",{"list":[newObject]}));
}
function getQrCode(id){
	$.post("${BASE_PATH}/auth/admin/getQrCode/"+id,function(result){
		if(result.flag==0){
			onEditSuccess(id,result.object);
		}else{
			Kit.alert("操作失败");return;
		}
		
	});
}
var editPwdDialog;
function toEditPwd(id){
	editPwdDialog = Kit.dialog("修改密码","${BASE_PATH}/auth/admin/toEditPwd/"+id).open();
}
function toDelete(id){
	Kit.confirm("提示","您确定要删除这条数据吗？",function(){
		$.post("${BASE_PATH}/auth/admin/doDelete/"+id,function(result){
			$("#item"+id).remove();
		});
	});
}
var sRoleDialog;
function toAccredit(id){
	sRoleDialog = Kit.dialog("授权","${BASE_PATH}/auth/admin/toAccredit/"+id,{closable:true,size:'size-wide'}).open();
}

$("#sBusId").change(function(){
	 var sBusId=[];
	  sBusId=$("#sBusId").val();
	  $.ajax({
               type: "POST",
               url: "${BASE_PATH}/bus/goods/base/chooseAllStore",
               data: {"sBusId" : sBusId},
               dataType: "json",
               traditional: true,
               success: function(data){
              	 $("#sStoreId").empty();
                         for(var i=0; i<data.object.length;i++){
                        	 $("#sStoreId").append("<option value='"+data.object[i].storeid+"'>"+data.object[i].title+"</option>");
                         }
                         $("#sStoreId").selectpicker("refresh");
                     }
           });
      }); 
</script>
</body>
</html>